<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/9/17
 * Time: 15:48
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\SuggestModel;
class Suggest extends Base
{
    /*
     * 用户反馈数据
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['user_username|f_phone'] = ['like',"%" . $key . "%"];
            }
            $map['f_where'] = 1;
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new SuggestModel();
            $lists = $user->getSuggestData($map, $Nowpage, $limits);
            $count = $user->getSuggestCount($map);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('suggest/index');
    }

    /*
     * 处理用户反馈
     */
    public function doUserSuggest(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'f_do_content'=>$extra,
                'f_do_time'=>date('Y-m-d H:i:s'),
                'f_status'=>2
            ];
            $user = new SuggestModel();
            $res = $user->save($param,['f_id' => $id]);
            if($res){
                return json(['code'=>200,'msg'=>'处理成功']);
            }else{
                return json(['code'=>100,'msg'=>'处理失败']);
            }
        }
        return $this->fetch('suggest/do_user_suggest',['id'=>input('id')]);
    }

    /*
     * 用户反馈处理结果
     */
    public function seeUserSuggest(){
        $user = new SuggestModel();
        $content = $user->where('f_id', input('id'))->value('f_do_content');
        return $this->fetch('suggest/see_user_suggest',['content'=>$content]);
    }


    /*
     * 回复消息
     */
    public function sendMsg(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'n_title'=>$n_title,
                'n_content'=>$n_content,
                'n_type'=>2,
                'n_app'=>1,
                'n_user_id'=>$user_id,
                'n_read'=>0,
                'n_time'=>date('Y-m-d H:i:s'),
                'n_admin'=>2
            ];
            $res = Db::name('notice')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'回复成功']);
            }else{
                return json(['code'=>100,'msg'=>'回复失败']);
            }
        }

        return $this->fetch('suggest/send_msg',['user_id'=>input('id')]);
    }

    /*
     * 商户反馈数据
     */
    public function business(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['s_name'] = ['like',"%" . $key . "%"];
            }
            $map['f_where'] = 3;
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new SuggestModel();
            $lists = $user->getBusinessData($map, $Nowpage, $limits);
            $count = $user->getBusinessCount($map);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('suggest/business');
    }
    /*
     * 处理商户反馈
     */
    public function doBusinessSuggest(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'f_do_content'=>$extra,
                'f_do_time'=>date('Y-m-d H:i:s'),
                'f_status'=>2
            ];
            $user = new SuggestModel();
            $res = $user->save($param,['f_id' => $id]);
            if($res){
                return json(['code'=>200,'msg'=>'处理成功']);
            }else{
                return json(['code'=>100,'msg'=>'处理失败']);
            }
        }
        return $this->fetch('suggest/do_businessr_suggest',['id'=>input('id')]);
    }

    /*
    * 商户反馈处理结果
    */
    public function seeBusinessSuggest(){
        $user = new SuggestModel();
        $content = $user->where('f_id', input('id'))->value('f_do_content');
        return $this->fetch('suggest/see_business_suggest',['content'=>$content]);
    }

    /*
     * 业务员反馈数据
     */
    public function salesman(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['user_name'] = ['like',"%" . $key . "%"];
            }
            $map['f_where'] = 2;
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new SuggestModel();
            $lists = $user->getSalesmanData($map, $Nowpage, $limits);
            $count = $user->getSalesmanCount($map);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('suggest/salesman');
    }

    /*
     * 处理业务员反馈
     */
    public function doSalesmanSuggest(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'f_do_content'=>$extra,
                'f_do_time'=>date('Y-m-d H:i:s'),
                'f_status'=>2
            ];
            $user = new SuggestModel();
            $res = $user->save($param,['f_id' => $id]);
            if($res){
                return json(['code'=>200,'msg'=>'处理成功']);
            }else{
                return json(['code'=>100,'msg'=>'处理失败']);
            }
        }
        return $this->fetch('suggest/do_salesman_suggest',['id'=>input('id')]);
    }

    /*
    * 业务员反馈处理结果
    */
    public function seeSalesmanSuggest(){
        $user = new SuggestModel();
        $content = $user->where('f_id', input('id'))->value('f_do_content');
        return $this->fetch('suggest/see_salesman_suggest',['content'=>$content]);
    }
}